﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace ClickToCall.CustomControls
{
    public partial class GradientLabel : System.Windows.Forms.Control
    {
        Bitmap gradientEdge, gradientInner;
        public GradientLabel()
        {
            InitializeComponent();
            gradientEdge = new Bitmap(
                    Properties.Resources.gradientedge,
                    new Size(
                        Properties.Resources.gradientedge.Width,
                        Properties.Resources.gradientedge.Height
                        )
                    );
            gradientInner = new Bitmap(
                    Properties.Resources.gradientinner,
                    new Size(
                        Properties.Resources.gradientinner.Width,
                        Properties.Resources.gradientinner.Height
                        )
                    );
        }

        private ContentAlignment _textAlign = ContentAlignment.MiddleCenter;
        public ContentAlignment TextAlign
        {
            get { return this._textAlign; }
            set
            {
                this._textAlign = value;
                this.Invalidate();
            }
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            Graphics g = pe.Graphics;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            
            TextureBrush gradientBrush = new TextureBrush(this.gradientInner, System.Drawing.Drawing2D.WrapMode.Tile);
            gradientBrush.TranslateTransform(gradientEdge.Width, 0);
            Rectangle rect = new Rectangle(0, 0, this.Width - gradientEdge.Width, this.Height);
            
            g.FillRectangle(gradientBrush, rect);
            g.DrawImage(gradientEdge, 0, 0);
            g.DrawImage(gradientEdge, this.Width, this.Height);
            

            if (this.Text.Length > 0)
            {
                SizeF sz = g.MeasureString(this.Text, this.Font);
                
                g.DrawString(this.Text,
                              this.Font,
                              new SolidBrush(this.ForeColor),
                              Common.GetTextPosition(this.TextAlign, this.ClientSize, sz)
                             );
            }

            base.OnPaint(pe);
            gradientBrush.Dispose();
            gradientBrush = null;
        }
    }
}
